package com.jiuwan.gamepass.service.userImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiuwan.gamepass.dao.authDao.SysUserDao;
import com.jiuwan.gamepass.dto.authDto.Result;
import com.jiuwan.gamepass.dto.authDto.ResultCode;
import com.jiuwan.gamepass.pojo.authPojo.SysUser;
import com.jiuwan.gamepass.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @version 1.0
 * 功能目的描述 ：用于         ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-06-28 17:54
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private SysUserDao sysUserDao;
        int i= 4;
    @Override
    public Result addUser(String username, String password, Integer role_id) {
        if (username == null | password == null | role_id == null) {
            Result error = Result.error(ResultCode.PARAM_NOT_BLANK);
            return error;
        }
        SysUser sysUser = sysUserDao.selectByUsername(username);
        if (sysUser != null) {
            Result error = Result.error(ResultCode.USER_ALREADY_EXIST);
            return error;
        }
        SysUser newSysUser = new SysUser();
        String encode = passwordEncoder.encode(password);
        newSysUser.setId(i);
        i++;
        newSysUser.setUsername(username);
        newSysUser.setPassword(encode);
        newSysUser.setRole_id(role_id);
        int i = sysUserDao.insertSelective(newSysUser);
        if (i != 1) {
            Result error = Result.error(ResultCode.COMMON_FAIL);
            return error;
        }
        return Result.ok();
    }

    @Override
    public Result deleteUser(String username) {
        if (username == null) {
            Result error = Result.error(ResultCode.PARAM_NOT_BLANK);
            return error;
        }
        if (sysUserDao.selectByUsername(username) == null) {
            Result error = Result.error(ResultCode.USER_ACCOUNT_NOT_EXIST);
            return error;
        }
        int i = sysUserDao.deleteByUsername(username);
        if (i != 1) {
            Result error = Result.error();
            return error;
        }
        return Result.ok();

    }

    @Override
    public Result updateUser(String username, String password, Integer role_id) {
        if (username == null) {
            Result error = Result.error(ResultCode.PARAM_NOT_BLANK);
            return error;
        }
        SysUser sysUser = sysUserDao.selectByUsername(username);
        String encode = passwordEncoder.encode(password);
        sysUser.setPassword(encode);
        sysUser.setRole_id(role_id);
        int i = sysUserDao.updateByPrimaryKeySelective(sysUser);
        if (i == 0) {
            Result error = Result.error();
            return error;
        }
        Result ok = Result.ok();
        return ok;
    }

    @Override
    public Result select(String username) {
        if (username == null) {
            List<SysUser> sysUsers = sysUserDao.selectAll();
            Result ok = Result.ok();
            HashMap<String, Object> map = new HashMap<>();
            map.put("users",sysUsers);
            ok.setData(map);
            return ok;
        }
        SysUser sysUser = sysUserDao.selectByUsername(username);
        if (sysUser==null){
            Result error = Result.error(ResultCode.USER_ACCOUNT_NOT_EXIST);
            return error;
        }
        Result ok = Result.ok();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<SysUser> list = new ArrayList<>();
        list.add(sysUser);
        map.put("user",list);
        ok.setData(map);
        return ok;
    }

    @Override
    public Result selectAll() {
        List<SysUser> sysUsers = sysUserDao.selectAll();
        Result ok = Result.ok();
        HashMap<String, Object> map = new HashMap<>();
        map.put("users",sysUsers);
        ok.setData(map);
        return ok;
    }

    public Result selectPage(String username, int current, int size) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        Page<SysUser> sysUserPage = sysUserDao.selectPage(new Page<>(current, size), wrapper.func(sysUserQueryWrapper -> {
            if (username != null) {
                sysUserQueryWrapper.eq("username", username);
            }
        }));

        Result ok = Result.ok();
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",sysUserPage);
        ok.setData(map);
        return ok;

    }
}
